package org.megha.blog.example.part18;

/**
 * A rectangle of given dimensions.
 * 
 * @author megha birmiwal
 */
public class Rectangle implements Shape {
	
	private final double length;
	private final double breadth;

	/** creates a Rectangle of the given dimensions */
	public Rectangle(double length, double breadth) {
		this.length = length;
		this.breadth = breadth;
	}
	
	public double getLength() {
		return length;
	}
	
	public double getBreadth() {
		return breadth;
	}
	
	@Override
	public double getArea() {
		return length * breadth;
	}
	
	@Override
	public double getPerimeter() {
		return(2 * (length + breadth));
	}

	@Override
	public int hashCode() {
		// TODO: fix this for small values of length/breadth; maybe use log()?
		return (int) (length * 31 + breadth);
	}
	
	@Override
	public String toString() {
		return "rectangle(l=" + length + ", b=" + breadth + ")";
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Rectangle)) {
			return false;
		}
		Rectangle that = (Rectangle) obj;

		return breadth == that.breadth && length == that.length;
	}
}
